﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using Common.Utilities;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace VideoMix.Views
{
    public partial class CreatePlayListPage : PhoneApplicationPage
    {
        public CreatePlayListPage()
        {
            InitializeComponent();
            Loaded += CreatePlayListPage_Loaded;
        }
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(MainGrid, adstackpl, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }

        void CreatePlayListPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAds();
            if (Constants.UpdatePlayListTitleAndDescrption == true)
            {
              int showid= Convert.ToInt32(AppSettings.ShowID);
              List<ShowList> showlist = Task.Run(async () => await Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).ToListAsync()).Result; 
                var item = showlist.FirstOrDefault();
                tbTitle.Text = item.Title;
                tbDescrption.Text = item.Description;
            }
        }

        private void imgclose_MouseLeftButtonDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

        }

        private void imgclose_Tapped(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

        }

        private void goBSave_Click(object sender, EventArgs e)
        {
            Constants.Gettileimage = false;            
            if (tbTitle.Text == "")
            {
                Titlemessage.Text = "please Enter  Title";
                Titlemessage.Visibility = Visibility.Visible;
            }
           
            else if (tbTitle.Text != "")
            {
                if (Constants.UpdatePlayListTitleAndDescrption == true)
                {
                  
                    DownloadManager.UpdatePlayListTitleAndDescrption(tbTitle.Text, tbDescrption.Text);
                    PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
                else
                {
                    OnlineVideos.Data.DownloadManager.SaveShowlistItems(tbTitle.Text, tbDescrption.Text);
                    PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
               
            }
            else
            {
                MessageBox.Show("please Enter Playlist Title and Playlist Descrption");
            }
            
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

            if (AppSettings.bcount)
            {
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
                //TODO: Use the onBackKeyPress in RatingPopup
                //ratingPopup.close();
            }

            AppSettings.detailtocast = 0;
          

            base.OnBackKeyPress(e);
        }

        private void appbariconaddvideos_Click_1(object sender, EventArgs e)
        {

        }

        private void tbTitle_MouseLeave_1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            bool PlayListTitleStatus;
            PlayListTitleStatus = DownloadManager.CompareCreateplayListTitle(tbTitle.Text);
            if (PlayListTitleStatus == true)
            {
                tbTitle.Text = "";
                Titlemessage.Text = "already exist  Playlist Title.";
                Titlemessage.Visibility = Visibility.Visible;
            }

        }

        private void tbTitle_MouseEnter_1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Titlemessage.Visibility = Visibility.Collapsed;
        }
    }
}